<?php
namespace Admin\Controller;
use Tools\AdminController;
class OrderController extends AdminController {
	function listquery(){
    	$this->display();
    }
	/*
	 * $acition代表当前要执行的操作，默认为8表示展示所有订单，
	 * $action=1，表示只要发货的订单，为4表示只要申请退款的订单
	 * */
	function listtable($action=8){//订单搜索
			if($action==1){//只要待发货订单
				$_POST['status']="1";
			}
			if($action==4){//只要待退款订单
				$_POST['status']="4";
			}
			$order=new \Model\OrderModel();	
			//分页相关
			$total=$order->count();
			$per=10;
			//实例化分页类对象
			$page_obj=new \Tools\CssPage($total,$per);
    	if(!empty($_POST)){
//			var_dump($_POST);
			$key=($_POST['key']!="")?" and {$_POST['keyword']}={$_POST['key']}":"";//管理员通过订单号或用户id搜索订单
			$status=($_POST['status']!="")?" and g.status={$_POST['status']}":"";//管理员通过订单状态筛选订单
//			echo $status;
			$sql="select g.*,u.name from goods_order as g,user as u where g.user_id=u.id ".$key.$status." order by create_time desc ".$page_obj->limit;
			
		}else{
			$sql="select g.*,u.name from goods_order as g,user as u where g.user_id=u.id order by create_time desc ".$page_obj->limit;
		}
//			echo $sql;
			$orders=$order->query($sql);
			//var_dump($info);
			$pagelist=$page_obj->fpage(array(3,4,5,6,7,8));
			$this->assign('total',$total);
			$this->assign('pagelist',$pagelist);	
			$this->assign('orders',$orders);
			$this->display();
    }
	function deleteorder($id){
		if(D('goodsorder')->delete($id)){
			$this->success('Index/success');
			$this->redirect('GoodsOrder/showlist',array(),3," ");
		}else{
			$this->error('Index/error');
			$this->redirect('GoodsOrder/showlist',array(),3," ");
		}
	}
	function assigngoods($id,$deliverynum,$deliverytype){
		$order=new \Model\GoodsOrderModel();	
		$status=D('goodsorder')->field('isok')->find($id);
		//echo $status['isok'];
		if($status['isok']==1){//取消发货，重置快递单号
			$sql="update goodsorder set isok=0,deliverynum=null,deliverytype=null where id=$id;";
			if($order->execute($sql)){
				echo "<i>发货</i>";
			}
		}else if($status['isok']==0){
			$sql="update goodsorder set isok=1,deliverynum=$deliverynum,deliverytype='$deliverytype' where id=$id;";
			if($order->execute($sql)){
				echo "<font color='green'>已发货</font>";
			}
		}else{
			echo "已完成";
		}	
			
	}
	//订单发货
	function sendgoods($uid,$delcom,$delnum){
		//echo $delcom;echo $delnum;
//		$this->ajaxReturn($delcom);
		$db=new \Model\DbModel();
		$res=$db->updatedata("goods_order",array("delivery_name"=>$delcom,"delivery_id"=>$delnum,"status"=>"2","send_time"=>$_SERVER['REQUEST_TIME']),"uid=".$uid);
		if($res){
			$this->ajaxReturn(array("errorcode"=>0,"error"=>"发货成功"));
		}else{
			$this->ajaxReturn(array("errorcode"=>1,"error"=>"发货失败"));
		}
	}
	//获取退款原因
	function getrefundreason($uid){
		$this->ajaxReturn(D('goods_refund')->field('reason')->where('order_uid='.$uid)->find()['reason']);
	}
	//退款处理
	function torefund($uid,$result){
		$db=new \Model\DbModel();
		if($result){//同意退款
			$db->startTrans();
			$res=$db->updatedata("goods_order",array("status"=>"7"),"uid=".$uid);
			$o=D('goods_order')->where('uid='.$uid)->find();
			//将积分退换给用户
			$res2=$db->updatedataforint("user",array("score"=>"score+".$o['paid_fee']),"id=".$o['user_id']);
			if($res&&$res2){
				$db->commit();
				$this->ajaxReturn("退款成功");
			}else{
				$db->rollback();
				$this->ajaxReturn(array("errorcode"=>1,"error"=>"退款失败，未知错误"));
			}
		}else{
			$res=$db->updatedata("goods_order",array("status"=>"5"),"uid=".$uid);
			$this->ajaxReturn("拒绝退款");
		}
	}
	//订单发货模块
	function waitsendorders(){
		$this->display();
	}
	//订单退款模块
	function waitrefundorders(){
		$this->display();
	}
}